<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <div class="filter-container">
          <el-row>
            <el-col type="flex" class="row-bg" justify="center" :span="18">
              <!-- 搜索 -->
              <el-form ref="searchRef">
                <el-form-item>
                  <strong>关键词</strong>
                  <el-input style="width: 200px;" placeholder="根据文章标题搜索" class="filter-item" v-model="query.keyword"></el-input>
                </el-form-item>
                <el-form-item>
                  <strong>状态</strong>
                  <el-select v-model="query.state" placeholder="请选择">
                    <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                  </el-select>
                </el-form-item>
                <el-form-item>
                  <el-button size="small" @click="reset">清除</el-button>
                  <el-button size="small" type="primary" @click="getlist">搜索</el-button>
                </el-form-item>
              </el-form>
            </el-col>
            <el-col :span="6">
              <el-button class="filter-item fr" size="small" style="margin-left: 10px;" type="success" icon="el-icon-edit" @click="addDialogFormVisible = true">新增技巧</el-button>
            </el-col>
          </el-row>
        </div>
        <el-alert v-if="counts !== ''" :title="'数据一共' + counts + '条'" type="info" class="alert" :closable="false" show-icon></el-alert>
        <!-- end -->
        <!-- 数据 -->
        <el-table :data="list" style="width: 100%">
          <el-table-column label="序号" type=index width="70">
          </el-table-column>
          <el-table-column label="文章列表" width="400">
            <template #default="{ row }">
              <span>{{ row.title }}</span>
              <a class="el-icon-film" v-show="row.videoURL"></a>
            </template>
          </el-table-column>
          <el-table-column label="阅读数">
            <template #default="{ row }">
              <span>{{ row.visits }}</span>
            </template>
          </el-table-column>
          <el-table-column label="录入人">
            <template #default="{ row }">
              <span>{{ row.username }}</span>
            </template>
          </el-table-column>
          <el-table-column label="录入时间" width="200">
            <template #default="{ row }">
              <span>{{ row.createTime | dateFormat }}</span>
            </template>
          </el-table-column>
          <el-table-column label="状态">
            <template #default="{ row }">
              <span v-if="row.state === 1">已启用</span>
              <span v-else>已禁用</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="200">
            <template #default="{ row }">
              <el-button type="text" @click="previewArt(row)">
                <span>预览</span>
              </el-button>
              <el-button type="text" @click="isAble(row)">
                <span v-if="row.state === 0">启用</span>
                <span v-else>禁用</span>
              </el-button>
              <el-button type="text" :disabled="row.state === 0" @click="openUpdateArt(row)">
                <span>修改</span>
              </el-button>
              <el-button type="text" :disabled="row.state === 0" @click="delArt(row)">
                <span>删除</span>
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- end -->
        <!-- 分页 -->
        <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="query.page" :page-sizes="[5,10,20,50]" :page-size="query.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="counts">
        </el-pagination>
        <!-- end -->
        <!-- 新增标签弹层 -->
        <AddArt ref="updateArt" :addDialogFormVisible.sync="addDialogFormVisible" />
        <PreviewArt :dialogVisible.sync="dialogVisible" :previewArtList.sync="previewArtList" />
      </el-card>
    </div>
  </div>
</template>

<script>
import { list as getList, remove as removeArt, changeState, detail } from '@/api/hmmm/articles.js'
import AddArt from '@/module-hmmm/components/articles-add.vue'
import PreviewArt from '@/module-hmmm/components/articles-preview.vue'
export default {
  components: {
    AddArt,
    PreviewArt
  },
  data () {
    return {
      options: [
        { label: '启用', value: 1 },
        { label: '禁用', value: 0 }
      ],
      // 文章预览弹层
      dialogVisible: false,
      // 文章预览数据
      previewArtList: {
        articleBody: '',
        createTime: '',
        creatorID: '',
        id: '',
        state: '',
        title: '',
        videoURL: '',
        visits: ''
      },
      // 新增文章弹层
      addDialogFormVisible: false,
      // 文章总数
      counts: 0,
      // 文章数据
      list: [],
      // 获取列表数据参数
      query: {
        page: 1,
        pagesize: 10,
        keyword: '',
        state: null
      }
    }
  },
  created () {
    this.getlist()
  },
  methods: {
    // 修改文章
    openUpdateArt (row) {
      this.addDialogFormVisible = true
      this.$refs.updateArt.updateArt(row)
    },
    // 文章预览
    async previewArt (row) {
      const { data } = await detail(row)
      // console.log(data)
      this.previewArtList = data
      this.dialogVisible = true
    },
    // 重置按钮
    reset () {
      this.query = {
        page: 1,
        pagesize: 10,
        keyword: '',
        state: null
      }
      this.getlist()
    },
    // 获取列表数据
    async getlist (params) {
      const list = await getList(this.query)
      // console.log(list)
      this.list = list.data.items
      this.counts = list.data.counts
    },
    // 切换文章状态
    async isAble (row) {
      try {
        if (row.state === 0) {
          row.state = 1
        } else {
          row.state = 0
        }
        await changeState(row)
        // console.log(list)
        this.$message.success('操作成功')
        this.getlist()
      } catch (error) {
        console.log(error)
      }
    },
    // 删除文章
    async delArt (row) {
      try {
        await this.$confirm(`确认要删除${row.title}吗`)
        await removeArt(row)
        this.$message.success('操作成功')
        this.getlist()
      } catch (error) {
        console.log(error)
      }
    },
    // 更改显示条数事件
    handleSizeChange (val) {
      this.query.pagesize = val
      this.getlist()
    },
    // 更改页码事件
    handleCurrentChange (val) {
      this.query.page = val
      this.getlist()
    }
  }
}
</script>

<style lang="scss" scoped>
.el-form-item {
  display: inline-block;
  vertical-align: top;
  margin-right: 10px;
  margin-bottom: 0;
  strong {
    vertical-align: top;
    padding-right: 12px;
    margin-left: 25px;
  }
  .el-input {
    padding: 0 12px 0 0;
  }
}
.el-icon-film {
  margin-left: 5px;
  font-size: 18px;
  color: #00f;
}
</style>
